import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-05-11
 * Time: 16:10
 */
public class Stack {

   public int[] elem;
   public int usedSize;

    public Stack() {
        this.elem = new int[10];
    }

    public boolean isFull() {
        return elem.length == usedSize;
    }
    public void push(int data) {
        if(isFull()) {
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = data;
    }

    public int pop() {
        if(usedSize == 0) {
            throw new IndexNullExcetion("越界异常");
        }
        //需要注意的是如果数组内部存入的是引用类型 得把它置空在减减  引用类型如果不置空会导致内存泄漏
        int ret = elem[usedSize-1];
        usedSize--;
        return ret;
    }

    public int peek() {
        if(usedSize == 0) {
            throw new IndexNullExcetion("越界异常");
        }

        int ret = elem[usedSize-1];
        return ret;
    }

    public int getUsedSize() {
        return usedSize;
    }
}
